/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ro.futurefeed.service;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import ro.futurefeed.api.FeedContainerTO;
import ro.futurefeed.api.FeedTO;
import ro.futurefeed.api.FeedsService;
import ro.futurefeed.model.Feed;
import ro.futurefeed.model.User;
import ro.futurefeed.repository.FeedRepository;
import ro.futurefeed.repository.UserRepository;
import ro.futurefeed.util.FeedAssembler;

/**
 *
 * @author Alex
 */
@Component
public class FeedsServiceImpl implements FeedsService {
    
    private static final Logger log = Logger.getLogger("FeedsService");
    
    @Autowired
    private FeedRepository feedRepository;
    
    @Autowired
    private UserService userService;

    @Override
    public FeedContainerTO getFeeds(String accessToken) {
        log.log(Level.FINE, "Get feeds for access token {0}", accessToken);
        User myFutureFeedUser = userService.getUserWithExtendedToken(accessToken);
        
        List<Feed> userFeeds = feedRepository.getUserFeeds(myFutureFeedUser);
        List<FeedTO> toFeeds = toTOListOfFeeds(userFeeds);
        
        FeedContainerTO container = new FeedContainerTO();
        container.setElements(toFeeds);
        return container;
    }

    @Override
    public Response createFeed(String accessToken, FeedTO feedTO) {
        
        User user = userService.getUserWithExtendedToken(accessToken);

        Feed entityFeed = FeedAssembler.toEntity(feedTO);
        entityFeed.setUserKey(user.getId());
        entityFeed = feedRepository.save(entityFeed);
            
        URI uri = null;
        try {
            uri = new URI("" + entityFeed.getId());
        } catch (URISyntaxException ex) {
            Logger.getLogger(FeedsServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return Response.created(uri).build();
    }

    @Override
    public Response updateFeed(String accessToken, String feedId, FeedTO feedTO) {
        User user = userService.getUserWithExtendedToken(accessToken);

        Feed oldEntityFeed = feedRepository.findById(Long.parseLong(feedId));
        if(oldEntityFeed == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        if(!oldEntityFeed.getUserKey().equals(user.getId())) {
            return Response.status(Response.Status.FORBIDDEN).build();
        }
        Feed entityFeed = FeedAssembler.toEntity(feedTO);
        entityFeed.setId(oldEntityFeed.getId());
        entityFeed.setUserKey(user.getId());

        feedRepository.update(entityFeed);
        
        return Response.status(Response.Status.OK).build();
    }

    @Override
    public Response deleteFeed(String accessToken, String feedId) {
        User user = userService.getUserWithExtendedToken(accessToken);

        Feed oldEntityFeed = feedRepository.findById(Long.parseLong(feedId));
        if(oldEntityFeed == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        if(!oldEntityFeed.getUserKey().equals(user.getId())) {
            return Response.status(Response.Status.FORBIDDEN).build();
        }
        feedRepository.delete(oldEntityFeed);
        
        return Response.status(Response.Status.OK).build();
    }

    private List<FeedTO> toTOListOfFeeds(List<Feed> userFeeds) {
        List<FeedTO> listFeedTo = new ArrayList<FeedTO>();
        
        for(Feed feed : userFeeds) {
            listFeedTo.add(FeedAssembler.toTO(feed));
        }
        return listFeedTo;
    }

    @Override
    public FeedTO loadDetail(String accessToken, String id) {
        User user = userService.getUserWithExtendedToken(accessToken);
        
        Feed entityFeed = feedRepository.findById(Long.parseLong(id));
        if(entityFeed == null) {
            throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build());
        }
        if(!entityFeed.getUserKey().equals(user.getId())) {
            throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).build());
        }
        return FeedAssembler.toTO(entityFeed);
    }
}
